<template>
    <view class="bgtmert">
        <uni-list>
        	<uni-list-item title="头像" @tap="paihhgs" :thumbs="usericon"></uni-list-item>
        	<uni-list-item title="昵称" :rightText="res.nickname" @tap="hf('/pages/user/userinfo/nickname')"></uni-list-item>
			<uni-list-item title="性别" :rightText="sextext" @tap="hf('/pages/user/userinfo/sex')"></uni-list-item>
			<uni-list-item title="地区" :rightText="res.region" @tap="hf('/pages/user/userinfo/region')"></uni-list-item>
        </uni-list>
    </view>
</template>
<script>
    export default {
        data() {
            return {
				usericon:'../../../static/img/usericon.png',
				res:'',
				sextext:'',
				imageid:'',
				addresss:{
					province:'',
					city:'',
					area:''
				},
				form:{
					province:'',
					city:'',
					area:''
				}
			}
        },
        components: {},
        methods: {
			async dengljse(id) {
				let cs = {}				
				cs.avatar=id				
				let jhsd = await this.get("/api/user", cs,'PUT')		
				
			},
			paihhgs(){
				let th=this
				let token = uni.getStorageSync('access_token')
				uni.chooseImage({
				    count: 1, //默认9
				    sourceType: ['album','camera'], //从相册选择
				    success: function (res) {
						console.log("图片",res.tempFiles[0].name)
						// th.imgsd = res.tempFilePaths[0]						
						uni.uploadFile({
							url: 'http://kenide.youfai.cn/api/images', //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							header:{
								Accept:'application/json',
								Authorization:token
							},
							formData: {
								// 'image': res.tempFiles[0].name,
								'type':'image'
							},
							success: (uploadFileRes) => {
								let uploads=JSON.parse(uploadFileRes.data)
								console.log(uploads);
								th.usericon=uploads.data.path
								th.imageid=uploads.data.id
								if(uploads.code==0){
									th.dengljse(uploads.data.id)
								}
							}
						});
				    }
				});
				// uni.chooseImage({
				// 	success(e) {
				// 		th.usericon = e.tempFilePaths[0]
				// 	}
				// })
			},
			async getuserinfo() {		
				let userinfo = await this.get('/api/user',{})
				this.res = userinfo.data
				uni.setStorageSync('uinfo',this.res)
				this.$store.commit('setuserinfo', userinfo.data)				
				console.log("请求个人信息")
			},
			async addressedit(){
				let cs = {}				
				cs.province = this.form.province
				cs.city=this.form.city
				cs.area=this.form.area
				let jhsd = await this.get("/api/user", cs,'PUT')			
				// if(jhsd.code==0){
				// 	uni.showToast({
				// 		title:"地区已修改"
				// 	})
				// }
			}
			
		},
		onLoad(){
			uni.$on('address',(params)=>{
			        if(params.type == 1){
						this.form.province = params.id
						this.addresss.province =  params.name
					}else if(params.type == 2){
						this.form.city = params.id
						this.addresss.city =  params.name
					}else{
						this.form.area = params.id
						this.addresss.area =  params.name
						this.addressedit()
					}
					this.res.region = this.addresss.province+' '+this.addresss.city+' '+this.addresss.area
			    })
		},
		onUnload(){
			uni.$off('address')
		},
		onShow() {
			this.getuserinfo()
			var that=this
			setTimeout(function(){
				that.res = that.$store.state.userinfo
				if(that.addresss.province){
					that.res.region = that.addresss.province+' '+that.addresss.city+' '+that.addresss.area
				}
				that.usericon=that.res.avatar
				let mjjn = ['未知','男','女']
				that.sextext = mjjn[that.res.sex]
				console.log(that.sextext)
			},300)
			
		},
        mounted() {}
    }

</script>
<style  scoped>

</style>
